package org.cy.websocket.stomp.messaging;

import org.cy.websocket.stomp.StompCommand;
import org.cy.websocket.stomp.StompHandler;
import org.cy.websocket.stomp.StompHeaders;
import org.cy.websocket.stomp.protocols.Versions;

/**
 * created in 2021/5/17
 *
 * @author cao.yong
 */
public class StompMessageConverter {

    public static StompMessage<byte[]> getStompMessage(StompHandler handler, Message<byte[]> message) {
        return new StompMessage<byte[]>() {
            @Override
            public void ack(StompHeaders headers) {
                String subscription = message.getHeaders().getSubscription();
                String messageId = Versions.V1_2.equals(handler.getConnectVersion())
                        ? message.getHeaders().getAck()
                        : message.getHeaders().getMessageId();
                handler.ack(messageId, subscription, headers);
            }

            @Override
            public void nack(StompHeaders headers) {
                String subscription = message.getHeaders().getSubscription();
                String messageId = Versions.V1_2.equals(handler.getConnectVersion())
                        ? message.getHeaders().getAck()
                        : message.getHeaders().getMessageId();
                handler.nack(messageId, subscription, headers);
            }

            @Override
            public StompCommand getCommand() {
                return message.getCommand();
            }

            @Override
            public StompHeaders getHeaders() {
                return message.getHeaders();
            }

            @Override
            public byte[] getPayload() {
                return message.getPayload();
            }
        };
    }
}
